/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function (root) {
  let isbanlanced = true
  const iter = (root) => {
    let heightL = 0, heightR = 0
    if (root == null) return 0
    else {
      heightL = iter(root.left)
      heightR = iter(root.right)
      if (Math.abs(heightL - heightR) > 1) isbanlanced = false
      return heightL > heightR ? heightL + 1 : heightR + 1
    }
  }
  iter(root)
  return isbanlanced
}